/*从代客下单跳转的*/
if (sessionStorage.getItem("Identification")) {
    $("#orderCode").val(sessionStorage.getItem("orderId"));
    getList();
    // alert(sessionStorage.getItem("orderId"))
    orderInfo('订单详情', 'prepare-info.html', sessionStorage.getItem("orderId"))
}

function IntegralOrder() {
    $.ajax({

        url:root.integralManagement.queryIntegralOrders,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "order_id":$("#order_id").val(),
            "product_code":$("#product_code").val(),
            "product_name":$("#product_name").val(),
            "created_at_begin":$("#timemin").val(),
            "created_at_end":$("#timemax").val(),
            "login_account":$("#login_account").val(),
            "order_status":$("#change_price").find("option:selected").val(),
            "page_size":"30",
            "page_num":"1"
        }),
        success: function (data) {
            sessionStorage.setItem("integralchange_price",'')
            couponlist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.integralManagement.queryIntegralOrders,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "page_num":api.getCurrent(),
                            "page_size":30,
                            "order_id":$("#order_id").val(),
                            "order_status":$("#change_price").find("option:selected").val(),
                            "product_code":$("#product_code").val(),
                            "product_name":$("#product_name").val(),
                            "created_at_begin":$("#timemin").val(),
                            "created_at_end":$("#timemax").val(),
                            "login_account":$("#login_account").val()
                        }),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist(data, index){

    // var j = 0;
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组


    if(data !=null && data != ''){
        var datashu =data.startIndex+1;


        $.each(data.result, function (i, field) {
            var datashus = datashu++
            var rowData = new Array();
            rowData.push(datashus);
            rowData.push(field.order_id);
            rowData.push(field.product_name);
            rowData.push(field.product_code);
            rowData.push(field.unit_name);
            rowData.push((field.point*100).toFixed(0));
            rowData.push(field.created_at);
            rowData.push(field.order_status);
            rowData.push(field.login_account);
            dataSource.push(rowData);

        });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {

                        var val =""
                        if (row[7] == 50) {
                            val = '待发货'
                        }else if (row[7] == 60) {
                            val = '待收货'
                        }else if (row[7] == 80) {
                            val = '已完成'
                        }
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[8]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function (data, type, row, meta) {
                        var val='';
                        if (row[7] == 50) {
                            val = '<a onclick="edit(\''+row[1]+'\')">去发货</a>'
                        }else {
                            val = '<a onclick="edit(\''+row[1]+'\')">查看明细</a>'
                        }
                        return val;
                    },
                    "targets": 9
                },


            ]
        });

    };
}
function edit(id){
    sessionStorage.setItem("id",id)
    sessionStorage.setItem("orderId", id);
    var index = layer.open({
        type: 2,
        title:"订单详情",
        content: "integralOrderDetail.html"+"?id="+id

    });
    layer.full(index);
}

/*订单备注鼠标移出事件*/
function over(id) {
    $("#mouseBox" + id).css({
        display: "inline"
    });
}

function out(id) {
    $("#mouseBox" + id).css({
        display: "none"
    });
}



/*跳转订单详情*/
function orderInfo(title, url, data) {
    sessionStorage.setItem("orderId", data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}




//自提点
myFunctions()

function myFunctions() {
    $("#selfMentionPointNames").val("")
    var user_data = {
        pWarehouseCode: $("#shippingWarehouse option:selected").val()

    }
    var succCallBack = function (data, status, response) {
        console.log(data)
        if (data.errorCode == "0") {
            var jsonObj = data.data;
            if (jsonObj == "" || $("#shippingWarehouse option:selected").val() == "") {
                $("#tuan").html("<li>无数据</li> ");
            } else {
                console.log(jsonObj)

                var optionstring = "";
                for (var i = 0; i < jsonObj.length; i++) {
                    optionstring += "<li onclick='relation(" + jsonObj[i].warehouseCode + ",\"" + jsonObj[i].selfMentionPointName + "\")' data-align=\"" + jsonObj[i].warehouseCode + "\" > " + jsonObj[i].selfMentionPointName + "</li>";
                }
                $("#tuan").html(optionstring);
                console.log(jsonObj)
            }
        } else {
            $("#tuan").html("<li>无数据</li> ");
        }
    }
    web.doAjax(root.orderInter.queryStoreAddressByPwarehouseCode, 'post', user_data, succCallBack);

}

//取光标
$("#selfMentionPointNames").focus(function () {
    $("#tuan").show()
});
$(document).bind("click", function (e) {
    //id为menu的是菜单，id为open的是打开菜单的按钮
    if ($(e.target).closest("#tuan").length == 0 && $(e.target).closest("#selfMentionPointNames").length == 0) {
        //点击id为menu之外且id不是不是open，则触发
        $("#tuan").hide()
    }
})

function relation(id, ce) {
    $("#selfMentionPointNames").val(ce).text(id)
    $("#tuan").hide()
    console.log($("#selfMentionPointNames").text())
}

var integralchange_price=sessionStorage.getItem("integralchange_price")
if(integralchange_price!=""){
    $("#change_price").val(integralchange_price)
    IntegralOrder()
}